//
//  ViewController.m
//  demo
//
//  Created by God on 16/4/13.
//  Copyright © 2016年 N-wei. All rights reserved.
//

#import "ViewController.h"
#import "NWloginViewController.h"
@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSArray *allImageNames;
@property(nonatomic,strong)UIPageControl *pageControl;

@end

@implementation ViewController

- (NSArray *)allImageNames{
    if (!_allImageNames) {
        _allImageNames = @[@"xitong",@"gaojin",@"yewu",@"ziyuan"];
    }
    return _allImageNames;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadscrollView];
}

-(void)loadscrollView {
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.frame = self.view.frame;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*self.allImageNames.count, scrollView.frame.size.height);
    for (NSInteger i=0; i<self.allImageNames.count; i++) {
        UIImage *image = [UIImage imageNamed:self.allImageNames[i]];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        //明确的设置imageView的位置及大小信息
        CGRect imageFrame = CGRectZero ;
        imageFrame.size = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
        imageFrame.origin = CGPointMake(i*scrollView.frame.size.width, 0);
        imageView.frame = imageFrame;
        //将图片视图添加到scrollView中
        [scrollView addSubview:imageView];
    }
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake((self.allImageNames.count-1)*scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button addTarget:self action:@selector(enterApp:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];
    
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    self.pageControl = pageControl;
    pageControl.frame = CGRectMake(0, self.view.frame.size.height-40-30, self.view.frame.size.width, 30);
    pageControl.numberOfPages = self.allImageNames.count;
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.userInteractionEnabled = NO;
    [self.view addSubview:pageControl];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    NSInteger index = round(point.x/self.scrollView.frame.size.width);
    self.pageControl.currentPage = index;
}

-(void)enterApp:(UIButton *)sender{
	NWloginViewController *HomeVc = [[NWloginViewController alloc]initWithNibName:@"NWloginViewController" bundle:nil];
	[UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:HomeVc];//先将root添加在navigation上
}

@end
